package com.example.sqlitetest.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sqlitetest.R;
import com.example.sqlitetest.db.MyDataBaseHelper;

import org.w3c.dom.Text;

public class InsertWorkActivity extends AppCompatActivity {
    private RadioGroup radio1;
    private RadioGroup radio2;
    private RadioGroup radio3;

    /*题目组件*/
    private TextView   math;
    private TextView   english;
    private TextView   chinese;

    public String answer1="";
    public String answer2="";
    public String answer3="";
    private RadioButton r1;
    private RadioButton r2;
    private RadioButton r3;
    private Button btn;
    String student_num = "";
    boolean turnoff = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_insertwork);

        //初始化数据库对象
        MyDataBaseHelper helper = new MyDataBaseHelper(this);//创建并初始化DBOpenHelper对
        final SQLiteDatabase db = helper.getWritableDatabase();//初始化SQLiteDatabase对象
        student_num = getIntent().getStringExtra("name");


        /*实例化*/
        radio2= (RadioGroup)findViewById(R.id.radio2);
        radio1 = (RadioGroup)findViewById(R.id.radio1);
        radio3 = (RadioGroup)findViewById(R.id.radio3);

        /*实例化*/
        math = (TextView)findViewById(R.id.math);
        chinese = (TextView)findViewById(R.id.chinese);
        english = (TextView)findViewById(R.id.english);




        /*从数据库中获取题*/
        Cursor sub_cursor = db.query("subject", null, null, null, null, null, null);

        if (sub_cursor.moveToFirst()) {
            do {
                math.setText("数学题："+sub_cursor.getString(sub_cursor.getColumnIndex("sub_2")));
                chinese.setText("语文题："+sub_cursor.getString(sub_cursor.getColumnIndex("sub_1")));
                english.setText("英语题："+sub_cursor.getString(sub_cursor.getColumnIndex("sub_3")));
            } while (sub_cursor.moveToNext());
        }


        btn =(Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {

                for(int i = 0;i<radio1.getChildCount();i++){
                    r1 = (RadioButton)radio1.getChildAt(i);
                    if(r1.isChecked()){
                        answer1 =  r1.getText().toString();
                    }
                }
                for(int i = 0;i<radio2.getChildCount();i++){
                    r2 = (RadioButton)radio2.getChildAt(i);
                    if(r2.isChecked()){
                        answer2 =  r2.getText().toString();
                    }
                }
                for(int i = 0;i<radio3.getChildCount();i++){
                    r3 = (RadioButton)radio3.getChildAt(i);
                    if(r3.isChecked()){
                        answer3 =  r3.getText().toString();
                    }
                }
                //Toast.makeText(InsertWorkActivity.this, answer1+" "+answer2+" "+answer3, Toast.LENGTH_SHORT).show();



                Cursor cursor = db.query("homework", null, null, null, null, null, null);



                if (cursor.moveToFirst()){
                    do {
                        String name = cursor.getString(cursor.getColumnIndex("username"));
                        if(name.equals(student_num)){
                            turnoff = false;
                        }
                    } while (cursor.moveToNext());
                }


                if(turnoff) {
                    if (!answer1.equals("") && !answer2.equals("") && !answer3.equals("")) {
                        ContentValues values = new ContentValues();
                        values.put("username", student_num);
                        values.put("math", answer1);
                        values.put("chinese", answer2);
                        values.put("english", answer3);
                        db.insert("homework", null, values);
                        values.clear();
                        Toast.makeText(InsertWorkActivity.this, "提交作业成功!", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(InsertWorkActivity.this, "提交的作业为空!", Toast.LENGTH_SHORT).show();
                    }
                }
                else{
                    Toast.makeText(InsertWorkActivity.this, "你已经提交过，不能再次提交！", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
